using System;
using System.Linq;

namespace Test.ConsoleProgram.Algorithm.Solution
{
    [TestDescription("算法: 0621. 任务调度器")]
    public class No0621_LeastInterval : AbsBaseTestItem
    {
        public override void OnTest()
        {
            Assert.TestExe(LeastInterval, new char[] { 'A', 'A', 'A', 'B', 'B', 'B' }, 2, 8);
        }

        public int LeastInterval(char[] tasks, int n)
        {
            int[] map = new int[26];
            foreach (char c in tasks)
                map[c - 'A']++;
            map = map.OrderBy(m => m).ToArray();
            int max_val = map[25] - 1;
            int idle_slots = max_val * n;
            for (int i = 24; i >= 0 && map[i] > 0; i--)
            {
                idle_slots -= Math.Min(map[i], max_val);
            }
            return idle_slots > 0 ? idle_slots + tasks.Length : tasks.Length;
        }
    }
}
